//
//  HomeViewController.m
//  YJlandlord
//
//  Created by 文彬 on 2018/6/24.
//  Copyright © 2018年 文彬. All rights reserved.
//

#import "HomeViewController.h"
#import "WBLoopScrollView.h"
#import "ItemSelectTableViewCell.h"
#import "HousesViewController.h"
#import "SpecialSellViewController.h"
#import "YiPaiViewController.h"
#import "SearchConditionViewController.h"

@interface HomeViewController ()<WBLoopScrollViewDelegate,
    UITableViewDelegate,
    UITableViewDataSource>

@property (strong, nonatomic)WBLoopScrollView *loopScrollView; //顶部轮播
@property (strong, nonatomic)UITableView *listTableView;
@property (strong, nonatomic)NSArray *itemDatas;


@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initPageControl];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark 页面相关
- (void)initPageControl{

    [self addNavLeftView];
    
    [self addNavTopView];
    
    [self initTableView];
}

-(void)addNavLeftView{
    UIView *navLeftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"长沙" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button setTitleColor:kTitleColor forState:UIControlStateNormal];
    [navLeftView addSubview:button];
    [button makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@-20);
        make.height.equalTo(@30);
        make.centerY.equalTo(navLeftView.centerY);
    }];
    
    UIButton *arrBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [arrBtn setBackgroundImage:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
    [navLeftView addSubview:arrBtn];
    [arrBtn makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@0);
        make.width.equalTo(@15);
        make.height.equalTo(@7);
        make.centerY.equalTo(navLeftView.centerY);
    }];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:navLeftView];
}

- (void)addNavTopView{
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0 ,SCREEN_WIDTH-100-50, 30)];
    
//    topView.layer.cornerRadius = 15;
//    topView.layer.borderColor = kSecTitleColor.CGColor;
//    topView.layer.borderWidth = 0.5;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"搜索目的地" forState:UIControlStateNormal];
    [button setTitleColor:kTitleColor forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    [topView addSubview:button];
    [button makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@-40);
        make.height.equalTo(@30);
        make.centerY.equalTo(topView.centerY);
    }];
    [button buttonClick:^{
        SearchConditionViewController *searchController = [[SearchConditionViewController alloc]init];
        searchController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:searchController animated:YES];
    }];
  
    self.navigationItem.titleView = topView;
}

- (void)initTableView{
    
    [self.view addSubview:self.listTableView];
    [self.listTableView makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(@0);
    }];
    
    UIView *headView = [[UIView alloc]initWithFrame:self.loopScrollView.bounds];
    [headView addSubview:self.loopScrollView];
    self.listTableView.tableHeaderView = headView; //直接设置成loopScrollView时不显示
    [self.loopScrollView layoutPages];
}
#pragma mark WBLoopScrollViewDelegate
//总共的页数
- (NSInteger)numOfTotalPageCount
{
    return 3;
}

//单个页面的布局
- (UIView*)viewForPageIndex:(NSInteger)index
{
    UIImageView *view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.loopScrollView.frame.size.height)];
    view.backgroundColor = [UIColor lightGrayColor];
    [view sd_setImageWithURL:[NSURL URLWithString:@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4036889747,3526425271&fm=27&gp=0.jpg"] placeholderImage:nil options:SDWebImageRetryFailed];
    return view;
}

#pragma mark 按钮点击
- (void)buttonClickHandle:(NSInteger)index{
    switch (index) {
        case 0: // 时租
        case 1:
        case 2:
            {
                HousesViewController *houseController = [[HousesViewController alloc]init];
                houseController.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:houseController animated:YES];
            }
            break;
        case 3://特卖
        {
            SpecialSellViewController *specailSellController  = [[SpecialSellViewController alloc]init];
            specailSellController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:specailSellController animated:YES];
        }
            break;
        case 4: //意拍
        {
            YiPaiViewController *yipaiController = [[YiPaiViewController alloc]init];
            yipaiController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:yipaiController animated:YES];
        }
            break;
     
        default:
            break;
    }
}

#pragma mark UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 220;
    }
    return 150;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.section==0){
        
        ItemSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemSelectTableViewCell" forIndexPath:indexPath];
        [cell setFont:[UIFont systemFontOfSize:15]];
        [cell setVerticalSpace:20];
        [cell setData:self.itemDatas oneLineCount:3];
        WeakObj(self)
        cell.itemClicked = ^(id param) {
            [selfWeak buttonClickHandle:[param integerValue]];
        };
        return cell;
    }
    
    static NSString *iden = @"celliden";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }

    UIImageView *imgView = [[UIImageView alloc]init];
    [imgView sd_setImageWithURL:[NSURL URLWithString:@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4036889747,3526425271&fm=27&gp=0.jpg"] placeholderImage:nil];
    imgView.contentMode = UIViewContentModeScaleAspectFill;
    imgView.clipsToBounds = YES;
    [cell.contentView addSubview:imgView];
    [imgView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
}


#pragma mark getter
- (WBLoopScrollView*)loopScrollView{
    if (_loopScrollView==nil) {
        _loopScrollView = [[WBLoopScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200) animationDuration:5];
        _loopScrollView.delegate = self;
        _loopScrollView.backgroundColor = [UIColor redColor];
    }
    
    return _loopScrollView;
}
- (UITableView*)listTableView{
    if (_listTableView==nil) {
        _listTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _listTableView.delegate = self;
        _listTableView.dataSource = self;
        _listTableView.sectionFooterHeight = 0;
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTableView.backgroundColor = [UIColor clearColor];
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;        [_listTableView registerNib:[UINib nibWithNibName:@"ItemSelectTableViewCell" bundle:nil] forCellReuseIdentifier:@"ItemSelectTableViewCell"];
    }
    
    return _listTableView;
}
-(NSArray*)itemDatas{
    if (_itemDatas==nil) {
        _itemDatas  = @[@{@"title":@"时租",@"pic":@"header_default"},
                        @{@"title":@"短租",@"pic":@"header_default"},
                        @{@"title":@"旅居",@"pic":@"header_default"},
                        @{@"title":@"特卖",@"pic":@"header_default"},
                        @{@"title":@"意拍",@"pic":@"header_default"},
                        @{@"title":@"扫一扫",@"pic":@"header_default"}];
    }
    return _itemDatas;
}

@end
